package com.qixin.api.serviceimpl;


import com.qixin.api.bean.*;
import com.qixin.api.mapper.ComChargeInfoMapper;
import com.qixin.api.service.ComCarChargeService;
import com.qixin.api.service.ComChargeInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.util.chargeInterfce.Charge.bean.ChargeDetails;
import com.qixin.util.chargeInterfce.Charge.bean.StopChargeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充电记录表 服务实现类
 * </p>
 *
 * @author 吕正睿
 * @since 2018-10-29
 */
@Service
public class ComChargeInfoServiceImpl extends ServiceImpl<ComChargeInfoMapper, ComChargeInfo> implements ComChargeInfoService {
	@Autowired
	ComCarChargeService comCarChargeService;
	@Override
	public ResultPage<ComChargeInfo> selectMyPage(VoPage voPage,HttpSession session) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
		paramMap.put("limit", voPage.getLimit());
		paramMap.put("userid",((ComPtUser)session.getAttribute("user")).getId());
		List<ComChargeInfo> list = baseMapper.selectMyPage(paramMap );
		Integer count = baseMapper.countMyPage(paramMap );
		ResultPage<ComChargeInfo>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return resultPage;
	}
	@Override
	public ResultPage<ComChargeInfo> selectMyPage3(VoPage voPage) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
		paramMap.put("limit", voPage.getLimit());
		List<ComChargeInfo> list = baseMapper.selectMyPage3(paramMap );
		Integer count = baseMapper.countMyPage(paramMap );
		ResultPage<ComChargeInfo>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return resultPage;
	}
	@Override
	public ResultPage<ComChargeInfo> search(Map map, VoPage voPage) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
			paramMap.put("limit", voPage.getLimit());
			List<ComChargeInfo> list = baseMapper.search(map, paramMap);
			Integer count = baseMapper.countMyPage(paramMap );
			ResultPage<ComChargeInfo>resultPage=new ResultPage<>();
			resultPage.setCode(0);
			resultPage.setCount(count);
			resultPage.setMsg("成功");
			resultPage.setData(list);
			return resultPage;

	}
	@Override
	public ResultPage<ComChargeInfo> query(Map map,VoPage voPage){
		Map<String,Object> paramMap=new HashMap<>();
		paramMap.put("page",(voPage.getPage()-1)*voPage.getLimit());
		paramMap.put("limit",voPage.getLimit());
		List<ComChargeInfo>list=baseMapper.query(map,paramMap);
		Integer count=baseMapper.countMyPage(paramMap);
		ResultPage<ComChargeInfo>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return  resultPage;
	}




	@Override
	public ComChargeInfo findAddressByWeChartMyOrder(  String connID) {
		return baseMapper.findAddressByWeChartMyOrder(connID);
	}
	@Override
	public ResultPage<ComChargeInfo> selectMoney(Map map,VoPage voPage) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
		paramMap.put("limit", voPage.getLimit());
		List<ComChargeInfo> list = baseMapper.selectMoney(map,paramMap );
		Integer count = baseMapper.countMyPage(paramMap );
		ResultPage<ComChargeInfo>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return resultPage;
	}

	@Override
	public boolean weChartInsert(Map map1,Long userId,String userPhone,String carPlant) {
		ComChargeInfo comChargeInfo = new ComChargeInfo();
		comChargeInfo.setSamplingTime(map1.get("EndTime") + "");
		comChargeInfo.setOrderNum(map1.get("StartChargeSeq") + "");
		comChargeInfo.setUserId(userId);
		comChargeInfo.setPhone(userPhone);
		comChargeInfo.setUsername(userPhone);
		comChargeInfo.setSoc(map1.get("Soc") + "");
		comChargeInfo.setRemark("该用户正在充电..还未结束充电");
		comChargeInfo.setConnectorinfoId(map1.get("ConnectorID") + "");
		comChargeInfo.setStartTime(map1.get("StartTime") + "");
		//关联查询出 该枪口的充电站地址 和充电站名称
		ComChargeInfo comChargeInfoForAddress = this.findAddressByWeChartMyOrder(map1.get("ConnectorID") + "");
		comChargeInfo.setStationAddress(comChargeInfoForAddress.getStationAddress());
		comChargeInfo.setStationName(comChargeInfoForAddress.getStationName());
		comChargeInfo.setCarPlant(carPlant);
		//添加到充电记录表
		boolean flag= this.insert(comChargeInfo);
		System.err.println("保存状态："+flag);
		return flag;
	}

	@Override
	public boolean weChartUpdate(StopChargeBean stopChargeBean,String carPlant) {
		ChargeDetails chargeDetails = stopChargeBean.getChargeDetails().get(0);
		Map map = new HashMap<>();
		map.put("order_num", stopChargeBean.getStartChargeSeq());
		List<ComChargeInfo> comChargeInfos = this.selectByMap(map);
		ComChargeInfo comChargeInfo = comChargeInfos.get(0);
		comChargeInfo.setRemark("已结束充电");
		comChargeInfo.setEndTime(chargeDetails.getDetailEndTime());
		comChargeInfo.setStartTime(chargeDetails.getDetailStartTime());
		comChargeInfo.setTotalMoeny(new BigDecimal(stopChargeBean.getTotalMoney()));
		comChargeInfo.setElecMoeny(new BigDecimal(stopChargeBean.getElecMoney()));
		comChargeInfo.setServiceMoeny(new BigDecimal(stopChargeBean.getSeviceMoney()));
		comChargeInfo.setTotalPower(stopChargeBean.getTotalPower());
		this.updateById(comChargeInfo);
		//充电结束 修改金额
		return  comCarChargeService.removeMoenyForWeChat(carPlant, new BigDecimal(stopChargeBean.getTotalMoney()));
	}
}
